// 调用获取默认环境的数据库的引用：
const db = wx.cloud.database()
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mine: true,
    nickName: "",
    avater: "/images/register.png",
    disabled: true,
    id: '',
  },

  //获取地理位置
  getLocation(){
    wx.getLocation({
      type: 'gcj02',
      success:(res)=>{
        this.longitude = res.longitude
        this.latitude = res.latitude
      }
    })
  },

  /**授权登录 */
  bindGetUserInfo(event){
    console.log(event)
    const mine = this.data.mine
    let userInfo = event.detail.userInfo;
    if(mine && userInfo){
      //插入数据
      db.collection('news').add({
         data : {
          avater: userInfo.avatarUrl,
          nickName: userInfo.nickName,
          signature: "",
          telephone: "",
          weixinNumber:"",
          friendList:[],
          links: 0,
          isOpen: true,
          time: new Date(),
          longitude: this.longitude,
          latitude: this.latitude,
          location: db.Geo.Point(this.longitude,this.latitude)
        },
        success:(res)=>{
          db.collection('news').doc(res._id).get().then(res=>{
            // console.log(res)
            app.userInfo =Object.assign(app.userInfo,res.data) 
            // console.log(app.userInfo)
            this.setData({
              avater:app.userInfo.avater,
              nickName:app.userInfo.nickName,
              mine:false,
              id:app.userInfo._id
            })
          })
        }
      })
    }
  },

  //获取信息
  getMessage(){
    console.log(app.userInfo._id)
    db.collection('message').where({
      userId : app.userInfo._id
    }).watch({
      onChange(snapshot){
        console.log(snapshot)
        const list = snapshot.docChanges[0].doc.list
        if(list.length){
          wx.showTabBarRedDot({
            index: 2,
          })
          app.message = list
        }else{
          wx.hideTabBarRedDot({
            index: 2,
          })
          app.message = []
        }
      },
      onError(err){

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getLocation()
    //调用云函数login
    wx.cloud.callFunction({
      name: 'login',
      data:{
        // a:1,
        // c:2
      },
      // success:(res)=>{
        // console.log(res)
      // }
    }).then((res)=>{
      //查询数据库
      // console.log(res)
      db.collection('news').where({
        _openid: res.result.openid
      }).get().then((res)=>{
        console.log(res)
        if(res.data.length){
          app.userInfo = Object.assign(app.userInfo , res.data[0])
          // console.log(app.userInfo)
          this.setData({
            avater:app.userInfo.avater,
            nickName:app.userInfo.nickName,
            mine:false,
            id: app.userInfo._id
          })
          this.getMessage()
        }else{
          mine:true
        }
        // console.log(res)
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      nickName:app.userInfo.nickName,
      avater:app.userInfo.avater,
      id: app.userInfo._id
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})